package ar.edu.sovietrussia.customcomponents.genericlistview;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.extensions.ajax.markup.html.repeater.data.table.AjaxFallbackDefaultDataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.markup.html.panel.EmptyPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

/**
 */
public class GenericDataTablePanel<T extends Serializable> extends Panel {

    private static final long serialVersionUID = 2221387847513262682L;

    private List<T> itemsList;

    private List<IColumn<T>> tableColumns;

    private List<String> itemProperties;

    private int maxRows;

    public GenericDataTablePanel(final String id, final List<T> items, final List<String> properties) {
        super(id);
        this.setItemsList(items);
        this.itemProperties = properties;
        this.tableColumns = new ArrayList<IColumn<T>>();
        this.maxRows = 5;
        this.addComponents();
    }

    private void addComponents() {

        this.initColumns();

        this.addDataTable("datatable");
    }

    private void initColumns() {
        for (String prop : this.getItemProperties()) {
            this.getTableColumns().add(new PropertyColumn<T>(Model.of(prop), prop));
        }

        this.getTableColumns().add(new AbstractColumn<T>(new Model<String>("Acciones")) {

            private static final long serialVersionUID = -3828268226883191945L;

            @Override
            public void populateItem(final Item<ICellPopulator<T>> cellItem, final String componentId,
                    final IModel<T> rowModel) {
                cellItem.add(GenericDataTablePanel.this.actionsPanel(componentId, rowModel));
            }

        });
    }

    protected Panel actionsPanel(final String componentId, final IModel<T> rowModel) {
        // REDEFINIR
        return new EmptyPanel(componentId);
    }

    private void addDataTable(final String id) {
        GenericProvider<T> provider = new GenericProvider<T>(this.getItemsList());

        AjaxFallbackDefaultDataTable<T> table = new AjaxFallbackDefaultDataTable<T>(id, this.getTableColumns(),
                provider, this.getMaxRows());

        this.add(table);
    }

    public void setTableColumns(final List<IColumn<T>> tableColumns) {
        this.tableColumns = tableColumns;
    }

    public List<IColumn<T>> getTableColumns() {
        return this.tableColumns;
    }

    public void setItemsList(final List<T> itemsList) {
        this.itemsList = itemsList;
    }

    public List<T> getItemsList() {
        return this.itemsList;
    }

    public void setItemProperties(final List<String> itemProperties) {
        this.itemProperties = itemProperties;
    }

    public List<String> getItemProperties() {
        return this.itemProperties;
    }

    public void setMaxRows(final int maxRows) {
        this.maxRows = maxRows;
    }

    public int getMaxRows() {
        return this.maxRows;
    }

}
